package com.at.atapp;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
import net.simonvt.widget.NumberPicker;
import net.simonvt.widget.NumberPicker.OnValueChangeListener;

import com.at.appmodel.TimeManagementModel;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.TimePickerDialog;
import android.os.Build;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
//import android.widget.NumberPicker;
import android.widget.Switch;
import android.widget.TimePicker;

public class TimeManagement extends Activity {

	Calendar dateTime= Calendar.getInstance();
	EditText timefrom,timeto;
	boolean	 timefrom_clicked;
	private int rememberInterval = 0;
	private TimeManagementModel oTimeManagementModel;
	private Switch alarm_switch;
	private CheckBox cbx_alarm_switch;		//Used for downgrade (Gingerbread)
	private CheckBox selectedweekdays[] = new CheckBox[7];
	Set<String> checkeddweekdays = new HashSet<String>();
	
	
	@SuppressLint("SimpleDateFormat")
	SimpleDateFormat formatDateTime = new SimpleDateFormat("HH:mm");
	
	
	public TimeManagement(){
	}
	
	@SuppressLint("NewApi")
	private void init(){
		
		oTimeManagementModel	= TimeManagementModel.getInstance(getBaseContext());
		
		checkeddweekdays	= oTimeManagementModel.getSpelectedweekdays();
		
		selectedweekdays[0]	= (CheckBox)findViewById(R.id.chkbox_so);
		selectedweekdays[1]	= (CheckBox)findViewById(R.id.chkbox_mo);
		selectedweekdays[2]	= (CheckBox)findViewById(R.id.chkbox_di);
		selectedweekdays[3]	= (CheckBox)findViewById(R.id.chkbox_mi);
		selectedweekdays[4]	= (CheckBox)findViewById(R.id.chkbox_do);
		selectedweekdays[5]	= (CheckBox)findViewById(R.id.chkbox_fr);
		selectedweekdays[6]	= (CheckBox)findViewById(R.id.chkbox_sa);
		
		
		int i = 0;
		for(CheckBox cbx : selectedweekdays){
			cbx.setOnCheckedChangeListener(chkbox_handler);
			if(checkeddweekdays.contains(TimeManagementModel.days[i])){
				cbx.setChecked(true);
			}
			i++;
		}
		
		
	    timefrom	= (EditText) findViewById(R.id.timefrom);
	    timefrom.setInputType(InputType.TYPE_NULL); 
	    timefrom.setOnClickListener(button_timefrom_handler);	    
	    
	    timeto	= (EditText) findViewById(R.id.timeto);
	    timeto.setInputType(InputType.TYPE_NULL); 
	    timeto.setOnClickListener(button_timeto_handler);
	    
   	 if(Build.VERSION.SDK_INT >=  Build.VERSION_CODES.HONEYCOMB){
	    alarm_switch	= (Switch)findViewById(R.id.switch_alarm);
	    alarm_switch.setOnCheckedChangeListener(alarm_switch_handler);
	    alarm_switch.setChecked(oTimeManagementModel.isAlarmActive());
   	 } else if(Build.VERSION.SDK_INT >=  Build.VERSION_CODES.GINGERBREAD_MR1){
   		cbx_alarm_switch	= (CheckBox)findViewById(R.id.switch_alarm);
   	   	cbx_alarm_switch.setOnCheckedChangeListener(alarm_switch_handler);
   	   	cbx_alarm_switch.setChecked(oTimeManagementModel.isAlarmActive());
	 }
   	 
		
		timefrom.setText(formatDateTime.format(oTimeManagementModel.getAlarmtimefrom().getTime()));
		timeto.setText(formatDateTime.format(oTimeManagementModel.getAlarmtimeto().getTime()));
		
	//	alarm_switch.setChecked(oTimeManagementModel.isAlarmActive());
	}
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_time_management);
	    
	    this.init();

	    
	    int minValue = 5;
	    int maxValue = 50;
	    
	    rememberInterval = oTimeManagementModel.getTimeInterval();
	    
	    if(rememberInterval == 0){
	    	rememberInterval	= maxValue / 2;
	    	oTimeManagementModel.setTimeInterval(rememberInterval);
	    }

	    NumberPicker numberpicker = (NumberPicker) findViewById(R.id.numberpicker_time_interval);
        numberpicker.setFocusable(true);
        numberpicker.setFocusableInTouchMode(true);
	    numberpicker.setMinValue(minValue);
	    numberpicker.setMaxValue(maxValue);
	    numberpicker.setValue(rememberInterval);
 
	    numberpicker.setOnValueChangedListener(number_picker_handler);
	    /*Button timefrom	= (Button) findViewById(R.id.timefrom);
	    timefrom.setOnClickListener(button_timefrom_handler);*/
	
	    
	}
	
	OnCheckedChangeListener	chkbox_handler	=	new OnCheckedChangeListener()
	{
	    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	    {
	    	
	    	

	    	if(isChecked){
		    	
	        	if(buttonView == selectedweekdays[0]){
	        		checkeddweekdays.add("So");
	        	}
	        	
	        	if(buttonView == selectedweekdays[1]){
	        		checkeddweekdays.add("Mo");
	        	}
		       
	        	if(buttonView == selectedweekdays[2]){
	        		checkeddweekdays.add("Di");
	        	}
	        	
	        	if(buttonView == selectedweekdays[3]){
	        		checkeddweekdays.add("Mi");
	        	}
	        	
	        	if(buttonView == selectedweekdays[4]){
	        		checkeddweekdays.add("Do");
	        	}
	        	
	        	if(buttonView == selectedweekdays[5]){
	        		checkeddweekdays.add("Fr");
	        	}
	        	
	        	if(buttonView == selectedweekdays[6]){
	        		checkeddweekdays.add("Sa");
	        	}
	        	

	    	}else{
	    		
	        	if(buttonView == selectedweekdays[0]){
	        		checkeddweekdays.remove("So");
	        	}	    		
	    		
		    	if(buttonView == selectedweekdays[1]){
	        		checkeddweekdays.remove("Mo");
	        	}
		       
	        	if(buttonView == selectedweekdays[2]){
	        		checkeddweekdays.remove("Di");
	        	}
	        	
	        	if(buttonView == selectedweekdays[3]){
	        		checkeddweekdays.remove("Mi");
	        	}
	        	
	        	if(buttonView == selectedweekdays[4]){
	        		checkeddweekdays.remove("Do");
	        	}
	        	
	        	if(buttonView == selectedweekdays[5]){
	        		checkeddweekdays.remove("Fr");
	        	}
	        	
	        	if(buttonView == selectedweekdays[6]){
	        		checkeddweekdays.remove("Sa");
	        	}
	        	
	    		
	    	}
	    	oTimeManagementModel.setSelectedweekdays(checkeddweekdays);
	    }
	};

	View.OnClickListener button_timefrom_handler = new View.OnClickListener() {
		  public void onClick(View v) {
			  timefrom_clicked	= true;
			  chooseTime();
		  }
	 };
	 
	View.OnClickListener button_timeto_handler = new View.OnClickListener() {
		  public void onClick(View v) {
			  timefrom_clicked	= false;
				
			  chooseTime();
		  }
	 };
	 
	 OnValueChangeListener number_picker_handler = new OnValueChangeListener() {

		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			// TODO Auto-generated method stub
			oTimeManagementModel.setTimeInterval(newVal);
		}
		//public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
	         
	        /* We display the month 1-12 but store it 0-11 so always
	         * subtract by one to ensure our internal state ispp always 0-11
	         */
			/*oTimeManagementModel.setTimeInterval(newVal);
		}*/

		/*@Override
		public void onScrollStateChange(NumberPicker picker, int newVal) {
			// TODO Auto-generated method stub
			oTimeManagementModel.setTimeInterval(newVal);
		}*/
	 };
	 
	 OnCheckedChangeListener alarm_switch_handler	= new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean on) {
				// TODO Auto-generated method stub
				oTimeManagementModel.setAlarmActive(on);
			}
		};
		
	 
    public void chooseTime(){

		new TimePickerDialog(TimeManagement.this, t, dateTime.get(Calendar.HOUR_OF_DAY), dateTime.get(Calendar.MINUTE), true).show();
    }
    
	TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener() {
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			dateTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
			dateTime.set(Calendar.MINUTE,minute);
			updateLabel();
		}
	};   
	
	private void updateLabel() {
		if(this.timefrom_clicked){
			
			oTimeManagementModel.setAlarmtimefrom(dateTime);
			timefrom.setText(formatDateTime.format(oTimeManagementModel.getAlarmtimefrom().getTime()));			
		}else{
			
			oTimeManagementModel.setAlarmtimeto(dateTime);
			timeto.setText(formatDateTime.format(oTimeManagementModel.getAlarmtimeto().getTime()));
		}
	}	
	
	@Override
	protected void onPause() {
			super.onPause();
		    // The SharedPreferences editor - must use commit() to submit changes
			oTimeManagementModel.saveModel();
	}	
}
